/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.client.activity;

import java.io.IOException;

import nu.zoom.android.gdata.project.client.Intents;
import nu.zoom.android.gdata.project.client.R;
import nu.zoom.android.gdata.project.client.StandardMenuHandler;
import nu.zoom.android.gdata.project.client.WKC;
import nu.zoom.android.gdata.project.integration.ProjectHostingClient;
import nu.zoom.android.gdata.project.integration.ProjectHostingClient.IssueOverview;
import nu.zoom.android.gdata.project.integration.ProjectHostingFactory;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class IssuesList
		extends
		AbstractRetainedListActivity<ProjectHostingClient.IssueOverviewFeed, ProjectHostingClient.IssueOverview> {
	private static final String LOG_TAG = WKC.LOG_TAG;
	private String projectName;
	private final StandardMenuHandler standardMenuHandler;

	public IssuesList() {
		super(R.layout.issuelist, R.id.issuepaginator);
		this.standardMenuHandler = new StandardMenuHandler(this);
	}

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final Intent intent = getIntent();
		projectName = intent.getStringExtra(Intents.Extra.PROJECT_NAME);
		if (projectName != null) {
			setTitle(projectName);
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		openIssue(position);
	}

	/**
	 * @param position
	 */
	private void openIssue(final int position) {
		IssueOverview issueOverview = adapter.getItem(position);
		openIssue(issueOverview.id);
	}

	/**
	 * @param id
	 */
	private void openIssue(final String id) {
		Log.d(LOG_TAG, "Sending intent to view issue with id: " + id);
		Intent intent = Intents.viewIssue(this, id, projectName);
		this.startActivity(intent);
	}

	@Override
	protected Dialog onCreateDialog(final int id) {
		return standardMenuHandler.onCreateDialog(id);
	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		standardMenuHandler.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.reloadandwebmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
		case R.id.reloadmenuitem: {
			startPosition = 1;
			runFeedQuery();
			return true;
		}
		case R.id.viewwebmenuitem: {
			Intent viewProjectWeb = Intents.viewProjectWeb(feedUri);
			this.startActivity(viewProjectWeb);
		}
		default:
			return standardMenuHandler.onOptionsItemSelected(item);
		}
	}

	@Override
	protected ProjectHostingClient.IssueOverviewFeed runInBackground(
			Uri uri,
			nu.zoom.android.gdata.project.client.activity.AbstractFeedrunnerListActivity.FeedParam parameter)
			throws IOException {
		return ProjectHostingFactory.getClient().getIssues(uri,
				parameter.startIndex, parameter.batchSize,
				parameter.cannedQuery);
	}

	@Override
	protected ArrayAdapter<IssueOverview> getAdapter() {
		return new ArrayAdapter<ProjectHostingClient.IssueOverview>(this,
				android.R.layout.simple_list_item_1);
	}

}
